package com.zhiyou.app.shop.manager.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.manager.action.vo.OrderVo;
import com.zhiyou.app.shop.manager.dao.IGoodsOrderDao;
import com.zhiyou.app.shop.manager.dao.IOrderDetailDao;
import com.zhiyou.app.shop.manager.dao.IPayOrderDao;
import com.zhiyou.app.shop.manager.dao.IShoppingTrolleyDao;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsOrder;
import com.zhiyou.app.shop.manager.dao.pojo.OrderDetail;
import com.zhiyou.app.shop.manager.dao.pojo.PayOrder;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.dao.pojo.UserDetail;
import com.zhiyou.app.shop.manager.service.IOrderService;
@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private IPayOrderDao payOrderDao;
	@Autowired
	private IGoodsOrderDao goodsOrderDao;
	@Autowired
	private IOrderDetailDao orderDetailDao;
	@Autowired
	private IShoppingTrolleyDao shoppingTrolleryDao;
	

	public String saveOrderFromJsonStr(String orderJson, UserDetail ud) {
//		Gson gson = new Gson();
//		PayOrder payOrder = gson.fromJson(orderJson, PayOrder.class);
//		Set<GoodsOrder> set = payOrder.getGoodsOrders();
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(orderJson);
		JsonObject json = element.getAsJsonObject();
		//解析支付订单
		String deleveryAddress = json.get("deliveryAddress").getAsString();
		String receipterPhone = json.get("receipterPhone").getAsString();
		String receipterName = json.get("receipterName").getAsString();
		Float totalPrice = json.get("totalPrice").getAsFloat();
		Set<GoodsOrder> goodsOrderSet = new HashSet<GoodsOrder>();
		PayOrder payOrder = new PayOrder(deleveryAddress, receipterPhone, receipterName, totalPrice);
		payOrder.setUserDetail(ud);
		payOrder.setCreateDate(new Date());
		payOrder.setGoodsOrders(goodsOrderSet);
		//解析商铺订单
		JsonArray shopOrders = json.get("goodsOrders").getAsJsonArray();
		for(JsonElement el : shopOrders){
			JsonObject shopOrder = el.getAsJsonObject();
			String shopId = shopOrder.get("shopId").getAsString();
			String memo = shopOrder.get("memo").getAsString();
			int payMode = shopOrder.get("payMode").getAsInt();
			Float orderPrice = shopOrder.get("orderPrice").getAsFloat();
			Set<OrderDetail> orderDetailSet = new HashSet<OrderDetail>();
			JsonArray orderDetails = shopOrder.get("orderDetails").getAsJsonArray();
			GoodsOrder order = new GoodsOrder(ud, payOrder, new Shop(shopId), Boolean.FALSE, payMode, Boolean.FALSE, 
					Boolean.FALSE, orderPrice, memo, new Date(), orderDetailSet);
			goodsOrderSet.add(order);
			//解析商铺订单详情
			for(JsonElement jd : orderDetails){
				JsonObject od = jd.getAsJsonObject();
				String goodsId = od.get("goodsId").getAsString();
				String goodsName = od.get("goodsName").getAsString();
				Float goodsPrice = od.get("goodsPrice").getAsFloat();
				int amount = od.get("amount").getAsInt();
				String goodsImg = od.get("goodsImg").getAsString();
				OrderDetail detail = new OrderDetail(order, goodsId, goodsName, goodsPrice, goodsImg, amount);
				orderDetailSet.add(detail);
			}
		}
		payOrderDao.save(payOrder);
		return payOrder.getId();
	}


	public void updateOrderPayStatus(String payOrderId, String useId) {
		//查询该支付订单下包含的所有商铺订单
		List<GoodsOrder> listOrder = goodsOrderDao.findByProperty("payOrder.id", payOrderId);
		for(GoodsOrder order : listOrder){
			//修改支付状态
			order.setIsPay(Boolean.TRUE);
			goodsOrderDao.update(order);
			//删除购物车相关记录
			shoppingTrolleryDao.deleteByOrderId(order.getId(),useId);
		}
	}

	public void findGoodsOrderForApp(Page page, GoodsOrder gd) {
		goodsOrderDao.findGoodsOrderByExample(page, gd);
		List<GoodsOrder> listOrder = page.getResult();
		if(! listOrder.isEmpty()){
			for(GoodsOrder order : listOrder){
				Set<OrderDetail> ds = order.getOrderDetails();
				Iterator<OrderDetail> it = ds.iterator();
				/*while(it.hasNext()){
					OrderDetail od = it.next();
				}*/
			}
		}
	}
	
	public void findMyShopOrder(Page page, GoodsOrder gd){
		goodsOrderDao.findMyShopOrder(page, gd);
	}


	public void setDeliverd(String id) {
		GoodsOrder go = goodsOrderDao.findById(id);
		go.setIsDelivery(Boolean.TRUE);
		goodsOrderDao.update(go);
	}


	public void setReceived(String orderId) {
		GoodsOrder go = goodsOrderDao.findById(orderId);
		go.setIsReceipt(Boolean.TRUE);
		goodsOrderDao.update(go);
	}


	public void findMyOrders(Page page, GoodsOrder order) {
		goodsOrderDao.findMyOrder(page, order);
	}

	public void deleteNoPaiedOrder(String id) {
		GoodsOrder go = goodsOrderDao.findById(id);
		goodsOrderDao.delete(go);
	}


	public void findAllShopOrders(Page page, OrderVo vo) {
		goodsOrderDao.findAllShopOrders(page, vo);
	}
	
	
}
